package com.classes;

import java.util.ArrayList;
import java.util.TreeMap;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.app.manager.DBConnectionManager;

public class StatusmastFactory {
	private static String message;
	
	public static String getMessage() {
		return "[StatusmastFactory] " + StatusmastFactory.message; 
	}
	
	public static ArrayList<TreeMap<String, String>> getActionStatuses(String current_status, String check_group, int branch) {
		ArrayList<TreeMap<String, String>> cArr = StatusmastFactory.getActionStatuses(current_status, branch);
		if(cArr == null){
			return null;
		}
		
		ArrayList<TreeMap<String, String>> cArr2 = new ArrayList<TreeMap<String, String>>();
		
		for(TreeMap<String, String> t : cArr){
			if(t.get("group") != null && t.get("group").equals("") == false) {
				ArrayList<String> g_arr = StatusmastFactory.parseGroup(t.get("group"));
				if(g_arr.contains(check_group)){
					cArr2.add(t);
				}
			}
			else {
				cArr2.add(t);
			}
		}
		
		return cArr2;
	}
	
	public static ArrayList<TreeMap<String, String>> getActionStatuses(String current_status, int branch) {
        String connectionPool = ConfigManager.getInstance().getConnectionPool();
        DBConnectionManager connMgr = DBConnectionManager.getInstance();
        Connection conn = connMgr.getConnection(connectionPool, 10000);
        
		ArrayList<TreeMap<String, String>> cArr = new ArrayList<TreeMap<String, String>>();
		PreparedStatement statement = null;
		try {
			String sql = "select (case when s_current = '*' then 0 else 1 end), statusmast.* from statusmast "
				+ "where (s_current = ? or s_current = '*') "
				+ "and s_branch = ? "
				+ "order by 1 asc, s_action asc";
			
			statement = conn.prepareStatement(sql);
			statement.setString(1, current_status);
			statement.setInt(2, branch);
			
			ResultSet resultSet = statement.executeQuery();
			
			while(resultSet.next()){
				TreeMap<String, String> c = StatusmastFactory.parseResultSet(resultSet);
				cArr.add(c);
			}
			
			return cArr;
			
		} catch (Exception e){
			StatusmastFactory.message = e.getMessage();
			return null;
		} finally {
        	if (statement != null) {
        		try { statement.close(); } catch (Exception e) {}
        	}
			connMgr.freeConnection(connectionPool, conn);
		}
	}
	
	public static TreeMap<String, String> getActionCode(String actionCode, int branch) {
        String connectionPool = ConfigManager.getInstance().getConnectionPool();
        DBConnectionManager connMgr = DBConnectionManager.getInstance();
        Connection conn = connMgr.getConnection(connectionPool, 10000);
        PreparedStatement statement = null;
		try {
			String sql = "select * from statusmast "
				+ "where s_action = ? "
				+ "and s_branch = ? ";
			
			statement = conn.prepareStatement(sql);
			statement.setString(1, actionCode);
			statement.setInt(2, branch);
			
			ResultSet resultSet = statement.executeQuery();
			
			TreeMap<String, String> c = new TreeMap<String, String>();
			
			while(resultSet.next()){
				c = StatusmastFactory.parseResultSet(resultSet);
				break;
			}
			
			return c;
			
		} catch (Exception e){
			StatusmastFactory.message = e.getMessage();
			return null;
		} finally {
        	if (statement != null) {
        		try { statement.close(); } catch (Exception e) {}
        	}
			connMgr.freeConnection(connectionPool, conn);
		}
	}
	
	protected static TreeMap<String, String> parseResultSet(ResultSet resultSet) throws Exception{
		TreeMap<String, String> c = new TreeMap<String, String>();
		
		c.put("currentStatus", resultSet.getString("s_current"));
		c.put("action", resultSet.getString("s_action"));
		c.put("nextStatus", resultSet.getString("s_next"));
		c.put("group", resultSet.getString("s_group") == null ? "" : resultSet.getString("s_group"));
		c.put("maxisCode", resultSet.getString("maxis_code") == null ? "" : resultSet.getString("maxis_code"));
		
		return c;
	}
	
	protected static ArrayList<String> parseGroup(String s_group){
		String s_arr[] = null;
		ArrayList<String> arr = new ArrayList<String>();
		
		s_arr = s_group.split(",");
		
		for(String s : s_arr){
			arr.add(s);
		}
		
		return arr;
		
	}
}